<{include file="top_v2.html" }>
</head>
<body>
<div id="wrap" class="pt bind-page">
    <div class="header">
        <{$title}>
        <a href="javascript:window.history.back();" class="back"></a>
    </div>
    <{if $isBindPhone}>
        <div class="tip">绑定手机号即可获得100积分。</div>
    <{/if}>

    <div class="section">
        <div class="ui-item">
            <input type="tel" maxlength="11" placeholder="请输入手机号" id="phone" value="<{$userinfo.bindPhone}>"/>
        </div>

        <div class="ui-item">
            <input type="tel" maxlength="11" placeholder="请输入手机验证码" id="code"/>
            <a href="javascript:void(0);" class="getCode">获取验证码</a>
        </div>
    </div>

    <div class="btn-group">
        <a href="javascript:void(0);" class="bind">绑定</a>
    </div>
</div>


</body>
</html>
<script>
    var goBindUrl= "<{U('User/goBind')}>";
    var smsUrl = "./api.php?m=api&c=Sms&a=sendSmsCode";
    var flag = 0;
    var url = "<{$url}>";

    $(function(){

        $(".bind").on("click", function () {
            var phone = $("#phone").val()
                    ,code = $("#code").val();
            if (!validatePhone(phone)) {
                tip('请填写正确的手机号码');
                return false;
            }
            if(code.length != 4){
                tip("请填写正确的验证码");
                return false;
            }
            $.getJSON(goBindUrl, {phone:phone,code:code}, function(data){
                if(data.status) {
                    tip(data.msg);
                }else{
                    if(data.firstBindPhone){
                        tip("手机绑定成功，积分+100");
                    }else{
                        tip('手机绑定成功！');
                    }

                    if(url) window.location.href = url;
                    else window.history.go(-1);
                    

                }
            });
        });

        $(".getCode").on('click', function () {
            var phone = $("#phone").val();
            if (!validatePhone(phone)) {
                tip('请输入正确的手机号码');
                return false;
            }
            $.getJSON(smsUrl, {phone:phone,bind:1}, function(data){
                tip(data.msg);
                if(data.status == 0) {
                    flag = false;
                    $('.getCode').html('<font>60</font>秒后获取');
                    timer = setInterval(countDown, 1000);
                }
            });
        });

        var timer;
        var flag = true;
        var countDown = function CountDown() {
            var maxtime = parseInt($(".getCode font").html());
            if(maxtime>0){
                --maxtime;
                $('.getCode font').html(maxtime);
            }else{
                clearInterval(timer);
                $('.getCode').html('获取验证码');
                flag = true;
            }
        }


    });


</script>